package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.CategoryBrandRelationMapper;
import com.bush.mall.product.entity.CategoryBrandRelationEntity;
import com.bush.mall.product.service.CategoryBrandRelationService;

/**
 * 品牌分类关联
 * @author RUN
 * @date 2022-11-28
 */
@Service(CategoryBrandRelationService.BEAN_ID)
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    /**
     * 查询品牌分类关联列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<CategoryBrandRelationEntity> page = new Page<>(current,size);
            QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("CategoryBrandRelationService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建品牌分类关联表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        try{
            Assert.notNull(categoryBrandRelation,"参数异常");
            boolean flag = this.save(categoryBrandRelation);
            if (flag) return Result.ok("品牌分类关联添加成功");
            throw new SaaSException("品牌分类关联添加失败");
        }catch(Exception e){
            log.error("CategoryBrandRelationService saveCategoryBrandRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改品牌分类关联表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        try{
            Assert.notNull(categoryBrandRelation,"参数异常");
            boolean flag = this.updateById(categoryBrandRelation);
            if (flag) return Result.ok("品牌分类关联修改成功");
            throw new SaaSException("品牌分类关联修改失败");
        }catch(Exception e){
            log.error("CategoryBrandRelationService updateCategoryBrandRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除品牌分类关联表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteCategoryBrandRelation(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("品牌分类关联删除成功");
            throw new SaaSException("品牌分类关联删除失败");
        }catch(Exception e){
            log.error("CategoryBrandRelationService deleteCategoryBrandRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}